// pages/detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collapseShow: false,
    cardLike: false,
    audioStatus: false,
    duration:0,
    audioCurTimeSec:"00",
    audioCurTimeMin:"00",
    audioAllTimeSec:"00",
    audioAllTimeMin:"00",
    sK:'',
    user:{
      mobile:'暂无手机号'
    },
  },
  openCollapse(){ 
    this.setData({
      collapseShow: this.data.collapseShow ? false : true
    })
  },
  cardLikeFunc(){
    this.setData({
      cardLike: this.data.cardLike ? false : true
    })
  },

  addPhoneContact(){
    wx.addPhoneContact({
      firstName: '胜',
      lastName: '邵',
      nickName: '月生',
      mobilePhoneNumber: '18757706790',
      weChatNumber: 'shaosheng_'
    })
  },

  getPhoneNumber(e) {
    let _this = this;
    wx.showLoading({
      title: '请稍候',
      mask: true
    })

    wx.checkSession({
      success: function(){
        console.log('sK suc')
      },
      fail:function(){
        console.log('sK fail')
      }
    })

    if (!this.data.sK){
      wx.showModal({
        title: '失败',
        content: '获取失败',
      })

      wx.hideLoading();
      return;
    }
    wx.request({
      url: "https://xcx.wzgaolaozhuang.com/app/index.php?i=12&c=getphone&a=getphone",
      method: "POST",
      dataType: 'json',
      data: { session_key: this.data.sK, iv: e.detail.iv, encryptedData: e.detail.encryptedData },
      complete: function (res) {
        if (res == null || res.data == null) {
          console.error('网络请求失败');
          return;
        } else {
          _this.setData({
            'user.mobile': res.data.phoneNumber
          })

          wx.showModal({
            title: '手机号',
            content: res.data.phoneNumber,
          })
        }

        wx.hideLoading();
      }
    })
  },

  audioPlay(){
    let _this = this;
    if( this.data.audioStatus ){
      this.innerAudioContext.pause();
    } else {
      this.innerAudioContext.play();
      this.innerAudioContext.onPlay((res) => {
        _this.updateTime()
        _this.setData({
          duration: _this.innerAudioContext.duration,
        })
      })
    }

    this.setData({
      audioStatus: this.data.audioStatus ? false : true
    })
  },

  updateTime(){
    let _this = this;
    let s = 0;
    let m = 0;
    let sCurT = "00";
    let mCurT = "00";
    this.innerAudioContext.onTimeUpdate((res) => {
      //更新时把当前的值给slide组件里的value值。slide的滑块就能实现同步更新

      let curT = parseInt(_this.innerAudioContext.currentTime);
      let a1 = curT / 60;

      if ( a1 < 1 ){
        if( curT < 10 ){
          sCurT = "0"+curT;
        } else {
          sCurT = curT
        }
      } else {
        a1 = Math.floor( a1 );
        let a2 = curT - a1 * 60;

        if( a2 < 10){
          sCurT = "0" + a2;
        } else {
          sCurT = a2;
        }

        if( a1 < 10) {
          a1 = "0" + a1;
        } 
        mCurT = a1;

      }
      
      _this.setData({
        audioCurTimeSec: sCurT,
        audioCurTimeMin: mCurT
      })
    })
  },

  registerAudioContext: function () {
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46';

    // 开始播放时间
    this.innerAudioContext.startTime = 0;

    // this.innerAudioContext.onCanplay(()=>{
    //   console.log(this.innerAudioContext.duration)
    // })

    setTimeout(() => {
      console.log(this.innerAudioContext.duration);
    }, 10000)

    this.innerAudioContext.onEnded((res) => {
      console.log('播放结束!');
    })

    this.innerAudioContext.onError((res) => {

      // 播放音频失败的回调

      console.log('播放音频失败' + res);

    })

    this.innerAudioContext.onPause((res) => {

      console.log('播放暂停!');

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    this.registerAudioContext();
    wx.getStorage({
      key: 'session_key',
      success(res) {
        _this.setData({
          sK: res.data
        })
      },
      fail(){
        wx.login({
          success: res => {
            wx.request({
              url: "https://xcx.wzgaolaozhuang.com/app/index.php?i=12&c=getphone&a=getphone",
              method: "POST",
              dataType: 'json',
              data: { code: res.code },
              complete: function (res) {
                wx.setStorage({
                  key: "session_key",
                  data: res.data.session_key
                })

                _this.setData({
                  sK: res.data.session_key
                })

                if (res == null || res.data == null) {
                  console.error('网络请求失败');
                  return;
                }
              }
            })

          }
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})